package com.qk.service.impl;

import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.mapper.DeptMapper;
import com.qk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: zhangchuang
 * @Date: 2025/09/29 15:22
 * @Description: 部门服务实现类
 */
@Service
@SuppressWarnings("all")
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> getList() {
        List<Dept> list = deptMapper.selectAll();
        return list;
    }

    @Override
    public void deleteById(Integer id) {
        deptMapper.deleteById(id);
    }

    @Override
    public Dept getById(Integer id) {
        Dept dept = deptMapper.selectById(id);
        return dept;
    }

    @Override
    public void updateById(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateById(dept);
    }

    @Override
    public PageResult<Dept> listPage(String name, Integer status, Integer page, Integer pageSize) {
        Integer total = deptMapper.getTotal(name, status);
        Integer offset = (page - 1) * pageSize;
        List<Dept> deptList = deptMapper.selectPage(name, status, offset, pageSize);
        PageResult<Dept> deptPageResult = PageResult.<Dept>builder()
                .total(total)
                .rows(deptList)
                .build();
        return deptPageResult;
    }

    @Override
    public void addDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insertDept(dept);
    }
}
